extension TimeX on DateTime {
  /// 用于生成适合后端的 String DateTime，例如：`2023-01-01 00:00:00`
  String get toStr {
    final monthStr = month.toString().padLeft(2, '0');
    final dayStr = day.toString().padLeft(2, '0');
    final hourStr = hour.toString().padLeft(2, '0');
    final minuteStr = minute.toString().padLeft(2, '0');
    final secondStr = second.toString().padLeft(2, '0');
    return '$year-$monthStr-$dayStr $hourStr:$minuteStr:$secondStr';
  }

  /// 生成年月日
  /// 例如：`2023-01-01`
  String get toStrDate {
    final monthStr = month.toString().padLeft(2, '0');
    final dayStr = day.toString().padLeft(2, '0');
    if (DateTime.now().year == year) return '$monthStr-$dayStr';
    return '$year-$monthStr-$dayStr';
  }

  /// 用于生成显示的日期
  String get toAgo {
    final now = DateTime.now();
    final diff = now.difference(this);
    final months = diff.inDays ~/ 30;
    if (months > 0) {
      return '$months月前';
    }
    final days = diff.inDays;
    if (days > 0) {
      return '$days天前';
    }
    final hours = diff.inHours;
    if (hours > 0) {
      return '$hours小时前';
    }
    final minutes = diff.inMinutes;
    if (minutes > 0) {
      return '$minutes分钟前';
    }
    return '刚刚';
  }


}